package com.fuful.service;

import com.fuful.domain.Users;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

public class UsersService {

    private JdbcTemplate jdbcTemplate;
    private String sql;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public Users isLogin(String uname, String upwd){
        sql = "select * from Users where uname = ?";
        List<Map<String,Object>> data = jdbcTemplate.queryForList(sql,uname);
        if(!data.isEmpty()){
            Map<String,Object> row = data.get(0);
            if(upwd.equals(row.get("upwd").toString())){
                Users users = new Users();
                users.setUname(uname);
                users.setUpwd(upwd);
                return users;
            }
        }
        return null;
    }

    public boolean updateUsersPassWord(String uname,String newPassword){
        sql = "update Users set upwd= ? where uname = ?";
        int flag = jdbcTemplate.update(sql,newPassword,uname);
        if(flag > 0){
            return true;
        }
        return false;
    }

}
